//
//  SubjectView.m
//  
//
//  Created by Christian Speich on 16.12.07.
//  Copyright 2007-2008 Christian Speich. All rights reserved.
//

#import <CoreFoundation/CoreFoundation.h>
#import <Foundation/Foundation.h>
#import "SubjectView.h"
#import "SubjectDetailView.h"
#import "SubjectsManager.h"
#import "Subject.h"

@implementation SubjectView

- (id) initWithFrame:(struct CGRect)frame
{
	return [self initWithFrame:frame andEditMode:NO];
}

- (id) initWithFrame:(struct CGRect)frame andEditMode:(BOOL)_editMode
{
	self = [super initWithFrame:frame];
	if (self != nil) {
		UITableColumn *col;
		UITableColumn *col2;
		
		editMode = _editMode;
		
		sectionList = [[UISectionList alloc] initWithFrame:
					   CGRectMake(0.0f, 0.0f, frame.size.width,
								  frame.size.height)];
		table = [sectionList table];
		col = [[UITableColumn alloc] initWithTitle: @"Subject" 
										identifier: @"subject" 
											 width: editMode?frame.size.width:
			   250.0f];
		if (!editMode) {
			col2 = [[UITableColumn alloc] initWithTitle: @"Average" 
											 identifier: @"average" 
												  width: 70.0f];
		}
		subjectCells = [[NSMutableArray alloc] initWithCapacity:1];
		averageCells = [[NSMutableArray alloc] initWithCapacity:1];
		detailViews = [[NSMutableArray alloc] initWithCapacity:1];
		navItem = [[UINavigationItem alloc] 
				   initWithTitle:NSLocalizedString(@"marks",
												   @"Label for Marks")];
		
		[table setShouldHideHeaderInShortLists:NO];
		[table setSeparatorStyle:1];
		[sectionList setDataSource:self];
		[table addTableColumn: col]; 
		if (!editMode)
			[table addTableColumn: col2];
		[table setDataSource: self];
		[table setDelegate: self];
		
		[self makeContent];
		
		[sectionList reloadData];
		[table reloadData];
		if (editMode)
			[table enableRowDeletion:YES animated:NO];
		[self addSubview: sectionList];
	}
	return self;
}

- (void) makeContent
{
	int i;
	int count;
	UIImageAndTextTableCell *subjectCell;
	UIImageAndTextTableCell *averageCell;
	Subject* obj;
	
	[subjectCells removeAllObjects];
	[averageCells removeAllObjects];
	[detailViews removeAllObjects];
	
	count = [[[SubjectsManager sharedInstance] mainSubjects] count];
	for (i = 0; i < count; i++) {
		obj = [[[SubjectsManager sharedInstance] mainSubjects] 
						 objectAtIndex:i];
		subjectCell = [[UIImageAndTextTableCell alloc] init];
		if (!editMode)
			averageCell = [[UIImageAndTextTableCell alloc] init];
		
		[subjectCell setTitle:[obj name]];
		
		if (!editMode) {
			[averageCell setTitle:[NSString stringWithFormat:@"%1.1f",
								   [obj average]]];
			[averageCell setShowDisclosure:YES animated:NO];
			[averageCells addObject:averageCell];
			[averageCell release];
		}
		else {
			[subjectCell setShowDisclosure:YES animated:NO];	
		}
		
		[subjectCells addObject:subjectCell];
		
		if (editMode) {
			
		}
		else {
			[detailViews addObject:[[SubjectDetailView alloc] 
									initWithFrame: [self frame] 
									andSubjectIndex:i]];
		}
		
		[subjectCell release];
	}
	
	count = [[[SubjectsManager sharedInstance] subjects] count];
	for (i = 0; i < count; i++) {
		obj = [[[SubjectsManager sharedInstance] subjects] 
						 objectAtIndex:i];
		subjectCell = [[UIImageAndTextTableCell alloc] init];
		if (!editMode)
			averageCell = [[UIImageAndTextTableCell alloc] init];
		
		[subjectCell setTitle:[obj name]];
		if (!editMode) {
			[averageCell setTitle:[NSString stringWithFormat:@"%1.1f",
								   [obj average]]];
			[averageCell setShowDisclosure:YES animated:NO];	
			[averageCells addObject:averageCell];
			[averageCell release];
		}
		else {
			[subjectCell setShowDisclosure:YES animated:NO];	
		}
		
		[subjectCells addObject:subjectCell];
		
		if (editMode) {
		}
		else {
			[detailViews addObject:[[SubjectDetailView alloc] 
									initWithFrame: [self frame] 
									andSubjectIndex:i+[[[SubjectsManager 
														 sharedInstance]
														mainSubjects] count]]];
		}
		
		[subjectCell release];
	}
	
	if (!editMode) {
		subjectCell = [[UIImageAndTextTableCell alloc] init];
		averageCell = [[UIImageAndTextTableCell alloc] init];
	
		[subjectCell setTitle:NSLocalizedString(@"totalAverage",
												@"Label for total Average")];
		[averageCell setTitle:[NSString stringWithFormat:@"%1.1f",
							   [[SubjectsManager sharedInstance] average]]];
	
		[subjectCells addObject:subjectCell];
		[averageCells addObject:averageCell];
	
		[subjectCell release];
		[averageCell release];
	}
}

- (void) refreshContent
{
	int i, count = [averageCells count];
	
	if (editMode)
		return;
	
	for (i = 0; i < count; i++) {
		UIImageAndTextTableCell *averageCell = [averageCells objectAtIndex:i];
		
		if (i < [[[SubjectsManager sharedInstance] mainSubjects] count]) {
			[averageCell setTitle:[NSString stringWithFormat:@"%1.1f",
								   [[[[SubjectsManager sharedInstance] 
									  mainSubjects] objectAtIndex:i] 
									average]]];
		}
		else if (i < [[[SubjectsManager sharedInstance] mainSubjects] count]
			+ [[[SubjectsManager sharedInstance] subjects] count]) {
			[averageCell setTitle:[NSString stringWithFormat:@"%1.1f",
								   [[[[SubjectsManager sharedInstance] 
									  subjects] objectAtIndex:i-
									 [[[SubjectsManager sharedInstance] 
									   mainSubjects] count]] 
									average]]];
		}
		else {
			[averageCell setTitle: [NSString stringWithFormat:@"%1.1f", 
							   [[SubjectsManager sharedInstance] average]]];
		}
	}
}

- (int) numberOfRowsInTable: (UITable *)table
{
    return [subjectCells count];
}

- (UITableCell*)table:(UITable*)_table cellForRow:(int)row column:(id)column
{
	if ([[column identifier] isEqualToString:@"subject"]) {
		return [subjectCells objectAtIndex:row];
	}
	else {
		return [averageCells objectAtIndex:row];
	}
}

- (int)numberOfSectionsInSectionList:(UISectionList *)aSectionList
{
    return 2;
}

- (NSString*)sectionList:(UISectionList *)aSectionList 
		  titleForSection:(int)section
{
	if (section == 0) {
		return NSLocalizedString(@"mainSubjects",
								 @"Label for Main Subjects");
	}
	else {
		return NSLocalizedString(@"otherSubjects",
								 @"Label for other Subjects");
	}
}

- (int)sectionList:(UISectionList *)aSectionList rowForSection:(int)section {
	if(section == 0)
		return 0;
	else
		return [[[SubjectsManager sharedInstance] mainSubjects] count];
}

- (void)tableRowSelected:(NSNotification *)notification
{	
	if ([table selectedRow] < [detailViews count])
	{
		[viewManager addView:[detailViews objectAtIndex:[table selectedRow]]];	
	}
}

- (void)reload
{
	[table reloadData];
}

- (void)becomesManager:(id)manager
{
	viewManager = (ViewManager*)manager;
}

- (void)becomesBack
{
	[self refreshContent];
	[table selectRow:-1 byExtendingSelection:NO withFade:NO 
	scrollingToVisible:NO];
}

- (UINavigationItem*)NavigationItem
{
	return navItem;
}

- (BOOL) table:(UITable*)_table canSelectRow:(int)row
{
	if (row < [detailViews count]) {
		return YES;
	}
	return NO;
}

- (BOOL)table:(UITable*)table canDeleteRow:( int)row 
{
	return YES;
}

- (void)table:(UITable*)_table deleteRow:(int)_row 
{
}

- (void) dealloc
{
	[navItem release];
	[table release];
	[sectionList release];
	[subjectCells release];
	[averageCells release];
	[detailViews release];
	[viewManager release];
	 
	[super dealloc];
}


@end
